package org.android.sss;

import org.android.facebook.Util;
import org.android.tools.Global;
import org.android.tools.Product;
import org.android.tools.SSSHelper;
import org.android.tools.Shop;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class AddNewProductActivity extends Activity implements AdapterView.OnItemSelectedListener{

	private EditText editTextName, editTextPrice, editTextBrand, editTextHowPacked, editTextInfo;
	private Spinner spinnerChoosedShops;
	private Button buttonAddNewShop, buttonShareOnWall;

	private TextView textViewInfoShop;
	private Shop choosenShop = null;
	private Button buttonYes, buttonNo;

	private String array_spinner[] = null;

	private Product product = null;


	/** Called when the activity is first created. */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_new_product);
		editTextName = (EditText)findViewById(R.id.editTextProductName);
		editTextBrand = (EditText)findViewById(R.id.editTextBrand);
		editTextPrice = (EditText)findViewById(R.id.editTextPrice);
		editTextHowPacked = (EditText)findViewById(R.id.editTextHowPacked);
		editTextInfo = (EditText)findViewById(R.id.editTextInfo);

		buttonAddNewShop = (Button)findViewById(R.id.buttonAddProduct);
		buttonShareOnWall = (Button)findViewById(R.id.buttonShareOnWall);
		spinnerChoosedShops = (Spinner)findViewById(R.id.spinnerAvailableShop);
		spinnerChoosedShops.setOnItemSelectedListener(this);

		buttonYes = (Button)findViewById(R.id.buttonYesAddNewProduct);
		buttonNo = (Button)findViewById(R.id.buttonNoAddNewProduct);

		textViewInfoShop = (TextView)findViewById(R.id.textViewInfoShopAddNewProduct);
		textViewInfoShop.setText(Global.infoSearchForProduct);

		if (Global.shops.length == 0) {
			Global.shops = SSSHelper.getShops();
			buttonYes.setText("Choose");
			buttonNo.setVisibility(Button.INVISIBLE);
		}
		else {
			buttonYes.setText("Yes");
			buttonNo.setVisibility(Button.VISIBLE);
		}
		setShops();

		buttonNo.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Global.shops = SSSHelper.getShops();
				buttonYes.setText("Choose");
				buttonNo.setVisibility(Button.INVISIBLE);
				setShops();
			}
		});

		buttonYes.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				editTextName.setEnabled(true);
				editTextBrand.setEnabled(true);
				editTextHowPacked.setEnabled(true);
				editTextInfo.setEnabled(true);
				editTextPrice.setEnabled(true);
				buttonAddNewShop.setVisibility(Button.VISIBLE);
			}
		});





		buttonAddNewShop.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (editTextName.getText().toString().equals("") || 
						editTextPrice.getText().toString().equals("") ||
						editTextInfo.getText().toString().equals("") ||
						editTextHowPacked.getText().toString().equals("") ||
						editTextBrand.getText().toString().equals("")) {
					Util.showAlert(AddNewProductActivity.this,"Information", "Fill all spaces!");
				}
				else {
					Double tmpDouble = Double.parseDouble(editTextPrice.getText().toString());
					if (tmpDouble.equals(0.0)) {
						Util.showAlert(AddNewProductActivity.this,"Information", "The price of the product must be positive!");
					}
					else {
						product = new Product(editTextName.getText().toString().toLowerCase(), 
								tmpDouble, "", 
								editTextInfo.getText().toString().toLowerCase(), editTextHowPacked.getText().toString().toLowerCase(), 
								editTextBrand.getText().toString().toLowerCase(), 
								0, true);
						int result = SSSHelper.addNewProduct(Global.fb_uid, product, choosenShop);
						if (result == 0) {
							Util.showAlert(AddNewProductActivity.this,"Information", "Product has already existed in the database!");

						}
						if (result == 1) {
							Util.showAlert(AddNewProductActivity.this, "Information", "Product was added correctly.");
							buttonShareOnWall.setVisibility(Button.VISIBLE);
						}
					}
					
				}
			}
		});

		buttonShareOnWall.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				Integer tmp = choosenShop.number;
				postOnWall("added new product:\nName: " + product.name + "\nPrice: " + product.price.toString() + " DKK" + "\nInfo: "
						+ product.info + "\nHow many: " + product.howPacket + "\nBrand: " + product.brand
						+ "\nShop: " + choosenShop.name +", "+choosenShop.street+" "+tmp.toString());
				buttonShareOnWall.setVisibility(Button.INVISIBLE);
			}
		});


	}

	public void postOnWall(String msg) {
		Log.d("Tests", "Testing graph API wall post");
		try {
			String response = Global.mFacebook.request("me");
			Bundle parameters = new Bundle();
			parameters.putString("message", msg);
			parameters.putString("description", "test test test");
			response = Global.mFacebook.request("me/feed", parameters, 
					"POST");
			Log.d("Tests", "got response: " + response);
			if (response == null || response.equals("") || 
					response.equals("false")) {
				Log.v("Error", "Blank response");
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	private void setShops() {
		array_spinner=new String[Global.shops.length];
		for (int i = 0; i < Global.shops.length; i++) {
			Integer numer = Global.shops[i].number;
			array_spinner[i] = Global.shops[i].name + ", " + Global.shops[i].street + " " + numer.toString();
		}

		ArrayAdapter adapter = new ArrayAdapter(AddNewProductActivity.this, android.R.layout.simple_spinner_item, array_spinner);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerChoosedShops.setAdapter(adapter);
	}

	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		// TODO Auto-generated method stub
		choosenShop = Global.shops[arg2];
	}

	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub


	}


}
